#!/usr/bin/env python
"""
Acorn-SDR (A Collaboration of Radio Nodes)
(c) Bob Cowdery (G3UKB) 2009

File: gtk_rx_proc.py
Date: April 2009

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:

Manages the rx processors (NR, NB, ANF currently) pop-up window

"""

#-----------------------------------------------------------------------
# System imports
import sys
from time import sleep
import subprocess

# Import GTK libraries
import pygtk
import gtk
import gtk.glade
import gobject

# Application imports
import utils

#======================================================================
# The Filters pop-up.
class RxProcGui:
  
    #---------------------------------------------------------------------
    def __init__(self, rx_proc_tree, caps, dsp_pub, persist_service, callback):
        self.rx_proc_tree = rx_proc_tree
        self.caps = caps
        self.dsp_pub = dsp_pub
        self.persist_service = persist_service
        self.callback = callback
        self.ignore = False
        
        #-----------------------------------------------------------------------
        # Wire up the events
        dic =   {
            # Processors
            "on_tb_bin_toggled" : self.bin,
            "on_tb_nr_toggled" : self.nr,
            "on_tb_nb_toggled" : self.nb,
            "on_tb_anf_toggled" : self.anf,
            
            "on_b_cancel_rx_proc_clicked" : self.cancel,
        }
        
        # This will connect up all the events in one hit
        self.rx_proc_tree.signal_autoconnect (dic)
    
        # Now we need a reference to all the controls
        self.bbin = self.rx_proc_tree.get_widget('tb_bin')
        self.bnr = self.rx_proc_tree.get_widget('tb_nr')
        self.bnb = self.rx_proc_tree.get_widget('tb_nb')
        self.banf = self.rx_proc_tree.get_widget('tb_anf')
        
        # Avoid circular events
        self.ignore = False
        
        self.w=self.rx_proc_tree.get_widget('RxProc')
        self.w.set_position(gtk.WIN_POS_MOUSE)

        # Widget map
        self.dict = {'RX_PROC_BIN':0,'RX_PROC_NR':1,'RX_PROC_NB':2,'RX_PROC_ANF':3}
        self.widgets = [self.bbin, self.bnr, self.bnb, self.banf]        
        
        # Adjust for capabilities
        for widget in self.widgets:
            widget.set_sensitive(False)
        allowed_rx_procs = self.caps.rsplit(',')
        for allowed_rx_proc in allowed_rx_procs:
            if allowed_rx_proc in self.dict:
                self.widgets[self.dict[allowed_rx_proc]].set_sensitive(True) 
    
    #--------------------------------------------------------------------- 
    # Public Interface
    #---------------------------------------------------------------------
    def show_window(self):
        self.w.show_all()
    
    #---------------------------------------------------------------------
    def hide_window(self):
        self.w.hide()
        
    #---------------------------------------------------------------------
    # GUI generated events (i.e. things that happen when you play with widgets)
    # Events for the mode buttons
    #---------------------------------------------------------------------  
    #---------------------------------------------------------------------  
    def bin(self, widget):
        if not self.ignore:
            if widget.get_active():
                state = True
            else:
                state = False
            self.updater('bin', state)
            self.w.hide()
    
    #---------------------------------------------------------------------  
    def nr(self, widget):
        if not self.ignore:
            if widget.get_active():
                state = True
            else:
                state = False
            self.updater('nr', state)
            self.w.hide()
            
    #---------------------------------------------------------------------  
    def nb(self, widget):
        if not self.ignore:
            if widget.get_active():
                state = True
            else:
                state = False
            self.updater('nb', state)
            self.w.hide()
            
    #---------------------------------------------------------------------  
    def anf(self, widget):
        if not self.ignore:
            if widget.get_active():
                state = True
            else:
                state = False
            self.updater('anf', state)
            self.w.hide()
    
    #--------------------------------------------------------------------- 
    def updater(self, proc, value):
        self.callback(proc, value)
        
    #---------------------------------------------------------------------  
    def cancel(self, widget):
        self.w.hide()
        
    #---------------------------------------------------------------------  
    # Update events
    #---------------------------------------------------------------------  
    def bin_update(self, data):
        if data:
            self.bbin.set_active(True)
        else:
            self.bbin.set_active(False)
    
    def nr_update(self, data):
        if data:
            self.bnr.set_active(True)
        else:
            self.bnr.set_active(False)
        
    def nb_update(self, data):
        if data:
            self.bnb.set_active(True)
        else:
            self.bnb.set_active(False)
        
    def anf_update(self, data):
        if data:
            self.banf.set_active(True)
        else:
            self.banf.set_active(False)
        

